//
//  AppController.m
//  sffs
//
//  Created by Simon Fell on 2/21/08.
//  Copyright 2008 Simon Fell. All rights reserved.
//

#import "AppController.h"
#import "ZKLoginController.h"
#import "zkSforceClient.h"
#import "zkUserInfo.h"
#import "SalesforceFileSystem.h"
#import "Preferences.h"

@implementation AppController

@synthesize statusText, mountPoint;

+ (void)initialize {
	NSMutableDictionary * defaults = [NSMutableDictionary dictionary];
	NSString *prod = [NSString stringWithString:@"https://www.salesforce.com"];
	NSString *test = [NSString stringWithString:@"https://test.salesforce.com"];
	
	NSMutableArray * defaultServers = [NSMutableArray arrayWithObjects:prod, test, nil];
	[defaults setObject:defaultServers forKey:@"servers"];
	[defaults setObject:prod forKey:@"server"];
	[Preferences addDefaults:defaults];

	[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}

-(void)dealloc {
	[mountPoint release];
	[statusText release];
	[login release];
	[super dealloc];
}

-(BOOL)loggedIn {
	return fs != nil;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	login = [[ZKLoginController alloc] init];
	[self showLogin:self];
}

-(IBAction)showLogin:(id)sender {
	[login showLoginWindow:sender target:self selector:@selector(didLogin:)];
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[fs unmount];
	id delegate = [fs delegate];
	[fs release];
	[delegate release];
	return NSTerminateNow;
}

-(void)didLogin:(ZKSforceClient *)sf {
	[self setStatusText:[NSString stringWithFormat:@"Logged in as %@", [[sf currentUserInfo] userName]]];
	SalesforceFileSystem *sfs = [[SalesforceFileSystem alloc] initWithSforceClient:sf];

	[self willChangeValueForKey:@"loggedIn"];
	fs = [[GMUserFileSystem alloc] initWithDelegate:sfs isThreadSafe:YES];

	NSString *icon = [NSString stringWithFormat:@"volicon=%@", [[NSBundle mainBundle] pathForResource:@"sffs" ofType:@"icns"]];
	NSArray *options = [NSArray arrayWithObjects:@"ro", @"volname=Salesforce", icon, nil];
	[fs mountAtPath:@"/Volumes/Salesforce" withOptions:options];

	NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
	[center addObserver:self selector:@selector(didMount:)
               name:kGMUserFileSystemDidMount object:nil];
	[center addObserver:self selector:@selector(didUnmount:)
               name:kGMUserFileSystemDidUnmount object:nil];
	[self didChangeValueForKey:@"loggedIn"];
}

- (void)didMount:(NSNotification *)notification {
	NSDictionary* userInfo = [notification userInfo];
	NSString* mountPath = [userInfo objectForKey:@"mountPath"];
	[self setMountPoint:mountPath];
  
	if ([[NSUserDefaults standardUserDefaults] boolForKey:P_OPEN_FINDER]) {
		NSString* parentPath = [mountPath stringByDeletingLastPathComponent];
		[[NSWorkspace sharedWorkspace] selectFile:mountPath
                   inFileViewerRootedAtPath:parentPath];
	}
}

- (void)didUnmount:(NSNotification*)notification {
  [[NSApplication sharedApplication] terminate:nil];
}

@end
